package com.abluedog.player;

import net.minecraft.scoreboard.*;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PlayerData {

    //总方块数次数
    private int totalBreakBlocksCount = 0;

    //此次保底破坏的方块次数
    private int breakBlocksCount = 0;

    //紫色 大于10次必出
    private int secondaryRewardCounter = 0;

    //保底抽奖记录
    private Map<Integer,Integer> prizeDrawRecord = new HashMap<>();

    //是否大保底
    private Boolean isGuaranteedWinning = false;

    private ServerScoreboard scoreboard;

    private ScoreboardObjective scoreboardObjective;

    public Map<Integer,Integer> getPrizeDrawRecord() {
        return prizeDrawRecord;
    }

    public int getSecondaryRewardCounter() {
        return secondaryRewardCounter;
    }

    public void setSecondaryRewardCounter(int secondaryRewardCounter) {
        this.secondaryRewardCounter = secondaryRewardCounter;
    }

    public void setPrizeDrawRecord(Map<Integer, Integer> prizeDrawRecord) {
        this.prizeDrawRecord = prizeDrawRecord;
    }

    public void addBreaksCount() {
        this.breakBlocksCount ++;
        this.totalBreakBlocksCount++;
        this.secondaryRewardCounter++;
    }

    public ScoreboardObjective getScoreboardObjective() {
        return scoreboardObjective;
    }

    public void setScoreboardObjective(ScoreboardObjective scoreboardObjective) {
        this.scoreboardObjective = scoreboardObjective;
    }

    public int getBreakBlocksCount() {
        return breakBlocksCount;
    }

    public void setTotalBreakBlocksCount(int totalBreakBlocksCount) {
        this.totalBreakBlocksCount = totalBreakBlocksCount;
    }

    public void setBreakBlocksCount(int breakBlocksCount) {
        this.breakBlocksCount = breakBlocksCount;
    }

    public int getTotalBreakBlocksCount() {
        return totalBreakBlocksCount;
    }

    public ServerScoreboard getScoreboard() {
        return scoreboard;
    }

    public void setScoreboard(ServerScoreboard scoreboard) {
        this.scoreboard = scoreboard;
    }

    public Boolean getGuaranteedWinning() {
        return isGuaranteedWinning;
    }

    public void setGuaranteedWinning(Boolean guaranteedWinning) {
        isGuaranteedWinning = guaranteedWinning;
    }

    public void createScoreboard(ServerPlayerEntity player) {
        ServerScoreboard scoreboard = player.server.getScoreboard();
        ScoreboardObjective scoreboardObjective = new ScoreboardObjective(scoreboard, player.getUuidAsString(), ScoreboardCriterion.DUMMY, Text.translatable("rs.scoreboard.title").formatted(Formatting.AQUA), ScoreboardCriterion.RenderType.INTEGER, true, null);
        scoreboard.addScoreboardObjective(scoreboardObjective);
        Team team = scoreboard.addTeam(player.getUuidAsString());
        Formatting color = Formatting.BLUE;
        team.setColor(color);
        scoreboard.addScoreHolderToTeam(player.getUuidAsString(), team);
        scoreboard.setObjectiveSlot(ScoreboardDisplaySlot.SIDEBAR, scoreboardObjective);
        ScoreAccess totalBreak = scoreboard.getOrCreateScore(ScoreHolder.fromName(Text.translatable("rs.scoreboard.total").getString()), scoreboardObjective);
        totalBreak.setScore(this.totalBreakBlocksCount);
        totalBreak.setDisplayText(Text.translatable("rs.scoreboard.total").formatted(Formatting.RED));
        ScoreAccess breakBlocks = scoreboard.getOrCreateScore(ScoreHolder.fromName(Text.translatable("rs.scoreboard.break").getString()), scoreboardObjective);
        breakBlocks.setScore(this.breakBlocksCount);
        breakBlocks.setDisplayText(Text.translatable("rs.scoreboard.break").formatted(Formatting.RED));
        this.setScoreboardObjective(scoreboardObjective);
        this.setScoreboard(scoreboard);
    }

    public void updateScoreboard() {
        ScoreAccess breakBlocks = scoreboard.getOrCreateScore(ScoreHolder.fromName(Text.translatable("rs.scoreboard.break").getString()), this.scoreboardObjective);
        ScoreAccess totalBreak = scoreboard.getOrCreateScore(ScoreHolder.fromName(Text.translatable("rs.scoreboard.total").getString()), this.scoreboardObjective);
        breakBlocks.setScore(this.breakBlocksCount);
        totalBreak.setScore(this.totalBreakBlocksCount);
    }
}
